package com.sanjishu.enterlab.electricity.sale.controller.sale;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.CompanyBak;
import com.sanjishu.enterlab.electricity.sale.domain.SaleApply;
import com.sanjishu.enterlab.electricity.sale.service.CompanyBakService;
import com.sanjishu.enterlab.electricity.sale.service.SaleApplyService;
import com.sanjishu.enterlab.electricity.sale.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("saleManage")
public class SaleManageController {

    @Autowired
    private SaleApplyService saleApplyService;
    @Autowired
    private CompanyBakService companyBakService;

    @RequestMapping(value = "apply/index", method = RequestMethod.GET)
    public String index(Integer page, Model model) {
        Page<SaleApply> saleApplyPage = saleApplyService.newSaleApply(1);
        HashMap<String, List<SaleApply>> applyHashMap = new HashMap<>();
        HashMap<String, CompanyBak> companyBakHashMap = new HashMap<>();
        if (saleApplyPage.getContent() != null  && saleApplyPage.getContent().size() > 0) {
            for (SaleApply saleApply: saleApplyPage.getContent()) {
                List<SaleApply> applyList = applyHashMap.get(saleApply.getCompanyBak().getId().toString());
                if (applyList == null || applyList.size() == 0) {
                    applyList = new ArrayList<>();
                }
                applyList.add(saleApply);
                applyHashMap.put(saleApply.getCompanyBak().getId().toString(), applyList);
                companyBakHashMap.put(saleApply.getCompanyBak().getId().toString(), saleApply.getCompanyBak());
            }
        }
        model.addAttribute("applyHashMap", applyHashMap);
        model.addAttribute("companyBakHashMap", companyBakHashMap);
        return "saleManage/applies/index";
    }

    @RequestMapping(value = "apply/pass", method = RequestMethod.POST)
    @ResponseBody
    public Response pass(@RequestParam(value = "companyUsers[]", required = false) String[] companyUsers) {
        saleApplyService.passApply(companyUsers);
        return Response.buildSuccess("关联成功。");
    }

    @RequestMapping(value = "apply/refuse", method = RequestMethod.POST)
    @ResponseBody
    public Response refuse(@RequestParam(value = "companyUsers[]", required = false) String[] companyUsers) {
        saleApplyService.refuseApply(companyUsers);
        return Response.buildSuccess("驳回成功。");
    }

    @RequestMapping(value = "sales", method = RequestMethod.GET)
    public String index(Model model) {
        Page<CompanyBak> companyBakPage = companyBakService.findSaleConnects(null, null, null, 1);
        model.addAttribute("pageData", companyBakPage);
        return "saleManage/sales/index";
    }

    @RequestMapping(value = "page", method = RequestMethod.POST)
    public String index(String searchText, Integer page, String columnName, Integer sortType, Model model) {
        Page<CompanyBak> companyBakPage = companyBakService.findSaleConnects(searchText, columnName, sortType, page);
        model.addAttribute("pageData", companyBakPage);
        model.addAttribute("searchText", searchText);
        return "saleManage/sales/page";
    }
}
